package com.yq.cp.controller.basics;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.Department;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.Position;
import com.yq.cp.entity.basics.model.StructureTree;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.User;
import com.yq.cp.service.basics.DepartmentService;
import com.yq.cp.service.basics.OrganizationalStructureService;
import com.yq.cp.service.basics.PositionService;
import com.yq.cp.utils.BeanCopyUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Resource
    OrganizationalStructureService organizationalStructureService;
    @Resource
    DepartmentService departmentService;
    @Resource
    PositionService positionService;
    /**
     * 站点-部门-职位
     * @param
     * @return
     */
    @PostMapping("treeDet")
    public ApiResult treeDet(@RequestBody PageVo vo){
        List<OrganizationalStructure> list = new ArrayList<>();
        User user = UserThreadLocal.get();
        if (Objects.isNull(user.getStructureId())&&Objects.isNull(user.getSiteId())){
            list = organizationalStructureService.list(new QueryWrapper<OrganizationalStructure>()
                    .eq("type", 6)
                    .eq("examine", 1));
        }
        if (Objects.isNull(user.getStructureId())&&!Objects.isNull(user.getSiteId())){
            OrganizationalStructure byId = organizationalStructureService.getById(user.getSiteId());
            list.add(byId);
        }
        if (!Objects.isNull(user.getStructureId())){
            OrganizationalStructure byId = organizationalStructureService.getById(user.getStructureId());
            if (byId.getType()==1){
                list = organizationalStructureService.list(new QueryWrapper<OrganizationalStructure>()
                        .eq("type", 6).eq("examine", 1)
                        .eq("channel_id",byId.getId()));
            }if (byId.getType()==2){
                list = organizationalStructureService.list(new QueryWrapper<OrganizationalStructure>()
                        .eq("type", 6).eq("examine", 1)
                        .eq("region_id",byId.getId()));
            }
            if (byId.getType()==3){
                list = organizationalStructureService.list(new QueryWrapper<OrganizationalStructure>()
                        .eq("type", 6).eq("examine", 1)
                        .eq("sheng_id",byId.getId()));
            }
            if (byId.getType()==4){
                list = organizationalStructureService.list(new QueryWrapper<OrganizationalStructure>()
                        .eq("type", 6).eq("examine", 1)
                        .eq("shi_id",byId.getId()));
            }if (byId.getType()==5){
                list = organizationalStructureService.list(new QueryWrapper<OrganizationalStructure>()
                        .eq("type", 6).eq("examine", 1)
                        .eq("qu_id",byId.getId()));
            }if (byId.getType()==6){
                list.add(byId);
            }
        }
        List<StructureTree> structureTrees = BeanCopyUtil.copyList(list, StructureTree.class);
        if (Objects.isNull(vo.getType())){
            if (!list.isEmpty()){
                for (StructureTree structure : structureTrees) {
                    List<Department> siteId = departmentService.list(new QueryWrapper<Department>()
                            .eq("site_id", structure.getId()));
                    List<StructureTree> structureTrees1 = BeanCopyUtil.copyList(siteId, StructureTree.class);
                    structure.setChildren(structureTrees1);
                    if (!siteId.isEmpty()){
                        for (StructureTree department : structure.getChildren()) {
                            List<Position> department_id = positionService.list(new QueryWrapper<Position>()
                                    .eq("department_id", department.getId()));
                            List<StructureTree> structureTrees2 = BeanCopyUtil.copyList(department_id, StructureTree.class);
                            department.setChildren(structureTrees2);
                        }
                    }
                }
            }
        }else if (vo.getType()==1){

        }else {

            if (!list.isEmpty()){
                for (StructureTree structure : structureTrees) {
                    List<Department> siteId = departmentService.list(new QueryWrapper<Department>()
                            .eq("site_id", structure.getId()));
                    List<StructureTree> structureTrees1 = BeanCopyUtil.copyList(siteId, StructureTree.class);
                    structure.setChildren(structureTrees1);
                    if (vo.getType()==2){

                    } else {
                        if (!siteId.isEmpty()) {
                            for (StructureTree department : structure.getChildren()) {
                                List<Position> department_id = positionService.list(new QueryWrapper<Position>()
                                        .eq("department_id", department.getId()));
                                List<StructureTree> structureTrees2 = BeanCopyUtil.copyList(department_id, StructureTree.class);
                                department.setChildren(structureTrees2);
                            }
                        }
                    }
                }
            }
        }
        Page<StructureTree> structureTreePage = new Page<>(1,99);
        structureTreePage.setRecords(structureTrees);
        return ApiResult.success(structureTreePage);
    }

}

